<%@page import="models.AdsDealModel"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreService"%>
<%@page import="com.google.appengine.api.blobstore.BlobKey"%>
<%@page import="com.google.appengine.api.images.ImagesServiceFactory"%>
<%@page import="com.google.appengine.api.images.ImagesService"%>
<%@page import="search.DealSearchData"%>
<%@page import="admin.data.CategoryData"%>
<%@page import="models.CategoryModel"%>
<%@page import="java.util.List"%>
<%@page import="admin.data.DealData"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="models.DealModel"%>
<%@page import="helpers.UrlHelper"%>
<%@page import="java.util.ArrayList"%>
<%	
	AdsDealModel adSpecial = (AdsDealModel)request.getAttribute("adSpecial");
	List<DealModel> homeAdvertising = (ArrayList<DealModel>) request.getAttribute("homeAdvertising");
	List<DealModel> listMenuRight = (ArrayList<DealModel>) request.getAttribute("listMenuRight");
	List<DealModel> listMenuTop = (ArrayList<DealModel>) request.getAttribute("listMenuTop");		
	String url = (String)request.getAttribute("url");	
	ImagesService imagesService = ImagesServiceFactory.getImagesService();
	BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
%>

<jsp:include page="/admin/layouts/header.jsp">
    <jsp:param name="language" value="1" />
</jsp:include>
<script type="text/javascript" src="/admin/js/list_deal.js"></script>
<script type="text/javascript" src="/admin/js/list_form.js"></script>
<script type="text/javascript">
<!--
var server = "<%=UrlHelper.getServerUrl(request)%>";
//-->
</script>
        <div class="maincontent noright">
        	<div class="maincontentinner">
            	
                <ul class="maintabmenu">
                	<li><a href="/admin/deal/manage">Thống kê Deal</a></li>
                	<li><a href="/admin/deal/list">Quản lý Deal</a></li>
                	<li><a href="/admin/deal/add">Tạo mới Deal</a></li>
                	<li  class="current"><a href="/admin/deal/add">Quản lý Deal Quảng cáo</a></li>
                </ul><!--maintabmenu-->
                
                <div class="content">                	
                   <br clear="all" /><br />    
               		<jsp:include page="/admin/elements/message.jsp"/>
               		<%
               			if(adSpecial!=null&&homeAdvertising!=null&&homeAdvertising.size()>0){
               				DealModel dealSpecial = homeAdvertising.get(0);
               				String img;               				
                    		String[] split = dealSpecial.getImageDeal().split(",");                    		
                			if(split.length>0){
                				img = split[0];			
                			}else{
                				img = dealSpecial.getImageDeal();
                			}                	
                			BlobKey blobKey = new BlobKey(img);   
                			try{
                				img = imagesService.getServingUrl(blobKey);
                			}catch(Exception e){
                				img = "";
                			}
               		%>
	               		<form method="post" enctype="multipart/form-data" action="<%= blobstoreService.createUploadUrl("/admin/ads/right") %>"  class="stdform stdform2" id="changeImage">
		               		<div class="contenttitle radiusbottom0">
			                	<h2 class="table"><span>Deal Quảng cáo Trang chủ</span></h2>
			                </div>
			                <p>
		                       	<label>Ảnh đại diện</label>
		                         <span class="field">		                         		
		                         		<img style="width: 123px" src="<%=img %>" alt="<%=dealSpecial.getShortName()%>" title="<%=dealSpecial.getShortName()%>" />
		                         </span>                          
							</p>
							<p>
		                       	<label>Ảnh menu trang chủ</label>
		                         <span class="field">		   
		                         <%
		                         	if(adSpecial.getImages().size()>0){
			                         	for(String image : adSpecial.getImages()){
			                         		blobKey = new BlobKey(image);   
			                    			try{
			                    				image = imagesService.getServingUrl(blobKey);
			                    			}catch(Exception e){
			                    				image = "";
			                    			}
			                    			if(image!=null&&image.length()>0){
			                         		%>
			                         		<img  src="<%=image %>" alt="<%=dealSpecial.getShortName()%>" title="<%=dealSpecial.getShortName()%>" />
			                         		<%
			                    			}
			                         	}			                         	
		                         	}
		                         %>                      				                         		
		                         		<br><br>
		                         		<input type="file" name="specialImages" class="smallinput">&nbsp;&nbsp;(980x50 pixels)	
		                         </span>                          
							</p>						
							 <p>
		                       	<label>Tên Deal</label>
		                         <span class="field">
		                         		<%=dealSpecial.getDealId()%> - <%=dealSpecial.getShortName() %>
		                         </span>                          
							</p> 
							<p>
		                       	<label>Trạng thái</label>
		                         <span class="field">
		                         		<%
									switch (dealSpecial.getStatus()) {
										case 0: 
											%>Chưa kích hoạt<%
											break;
										case 1:
											%>Đang kích hoạt<%
											break;
										case 2:
											%>Đang chạy<%
											break;
										case 3:
											%>Kết thúc<%
											break;
										case 4:
											%>Đã khóa<%
											break;
										case 5:
											%>Hoàn thành<%
											break;
										default:
											%>Chưa kích hoạt<%
											break;											
									}
									%>
		                         </span>                          
							</p> 	
							<p>
	                        	<label></label>
	                        	<span class="field">        
	                        		<button class="submit radius2">Lưu Deal</button>                            	
	                            </span>
	                        </p>  		                
			            </form>
		                <br/>
	                <%
               			}
	                %>
	                
               		<%
               			if(listMenuTop!=null&&listMenuTop.size()>0){
               		%>
	                <div class="contenttitle radiusbottom0">
	                	<h2 class="table"><span>Danh sách Deal Quảng cáo Menu Top</span></h2>
	                </div>	                	               
	                
	                <table cellspacing="0" cellpadding="0" border="0" class="stdtable stdtablecb" id="list-table">
	                    <colgroup>
	                        <col class="con0">
	                        <col class="con1">
	                        <col class="con0">
	                        <col class="con1">
	                        <col class="con0">
	                        <col class="con1">
	                    </colgroup>
	                    <thead>
	                        <tr>	                        	
	                        	<th class="head0">Ảnh đại diện</th>
	                            <th class="head1">Mã Deal</th>
	                            <th class="head0">Tên Deal</th>	                            	                            
	                            <th class="head1">Trạng thái</th>
	                            <th class="head0">Thao tác</th>
	                        </tr>
	                    </thead>	                   
	                    <tbody>
						<%							
								for (int i = 0; i < listMenuTop.size(); i++) { 
	                    		String img;
	                    		String[] split = listMenuTop.get(i).getImageDeal().split(",");
	                			if(split.length>0){
	                				img = split[0];			
	                			}else{
	                				img = listMenuTop.get(i).getImageDeal();
	                			}                	
	                			BlobKey blobKey = new BlobKey(img);   
	                			try{
	                				img = imagesService.getServingUrl(blobKey);
	                			}catch(Exception e){
	                				img = "";
	                			}
	                    	%>	                    	
	                        <tr>	 
	                        	<td class="center">
	                        		<a href="/<%=listMenuTop.get(i).getDealId()%>/<%=listMenuTop.get(i).getAlias()%>.html" target="blank">
	                        			<img style="width: 123px" src="<%=img %>" alt="<%=listMenuTop.get(i).getShortName()%>" title="<%=listMenuTop.get(i).getShortName()%>" />
	                        		</a>
	                        	</td>                       
								<td class="center"><%=listMenuTop.get(i).getDealId() %></td>
	                            <td><%=listMenuTop.get(i).getShortName() %></td>	                            
	                            <td class="center">
	                            	<%
									switch (listMenuTop.get(i).getStatus()) {
										case 0: 
											%>Chưa kích hoạt<%
											break;
										case 1:
											%>Đang kích hoạt<%
											break;
										case 2:
											%>Đang chạy<%
											break;
										case 3:
											%>Kết thúc<%
											break;
										case 4:
											%>Đã khóa<%
											break;
										case 5:
											%>Hoàn thành<%
											break;
										default:
											%>Chưa kích hoạt<%
											break;											
									}
									%>
	                            </td>
	                            <td class="center">
									<a title="Edit Link" href="/admin/deal/edit?id=<%=listMenuTop.get(i).getDealId()%>" class="edit" target="blank">
										Sửa
									</a>&nbsp;|							
									<a title="Remove Link" href="<%=listMenuTop.get(i).getDealId()%>" id="action_del" class="btnRemoveTop">
										Xóa khỏi menu
									</a>
								</td>
	                        </tr>
	                        <% } 	                       
	                        %>
	                        
	                    </tbody>
	                </table>
	                <%
               			}
	                %>
	               	<br/> 	
               	  <%
                    	if(listMenuRight!=null&&listMenuRight.size()>0){
                    %>
                	<div class="contenttitle radiusbottom0">
	                	<h2 class="table"><span>Danh sách Deal Quảng cáo Menu bên phải</span></h2>
	                </div>	                	               
	                
	                <table cellspacing="0" cellpadding="0" border="0" class="stdtable stdtablecb" id="list-table">
	                    <colgroup>
	                        <col class="con0">
	                        <col class="con1">
	                        <col class="con0">
	                        <col class="con1">
	                        <col class="con0">
	                        <col class="con1">
	                    </colgroup>
	                    <thead>
	                        <tr>	                        	
	                            <th class="head0">Ảnh đại diện</th>
	                            <th class="head1">Mã Deal</th>
	                            <th class="head0">Tên Deal</th>	                            	                            
	                            <th class="head1">Trạng thái</th>
	                            <th class="head0">Thao tác</th>
	                        </tr>
	                    </thead>	                   
	                    <tbody id="sortable">
	                  
	                    	<% for (int i = 0; i < listMenuRight.size(); i++) { 
		                    		String img;
		                    		String[] split = listMenuRight.get(i).getImageDeal().split(",");
		                			if(split.length>0){
		                				img = split[0];			
		                			}else{
		                				img = listMenuRight.get(i).getImageDeal();
		                			}                	
		                			BlobKey blobKey = new BlobKey(img);   
		                			try{
		                				img = imagesService.getServingUrl(blobKey);
		                			}catch(Exception e){
		                				img = "";
		                			}
	                    	%>
	                        <tr id="<%=listMenuRight.get(i).getDealId()%>" title="Kéo thả để thay đổi vị trí sắp xếp các Deal menu phải">
	                        	<td class="center">
	                        		<a href="/<%=listMenuRight.get(i).getDealId()%>/<%=listMenuRight.get(i).getAlias()%>.html" target="blank">
	                        			<img style="width: 123px" src="<%=img %>" alt="<%=listMenuRight.get(i).getShortName()%>" title="<%=listMenuRight.get(i).getShortName()%>" />
	                        		</a>
								</td>	                        
								<td class="center"><%=listMenuRight.get(i).getDealId() %></td>
	                            <td><%=listMenuRight.get(i).getShortName() %></td>	                           
	                            <td class="center">
	                            	<%
									switch (listMenuRight.get(i).getStatus()) {
										case 0: 
											%>Chưa kích hoạt<%
											break;
										case 1:
											%>Đang kích hoạt<%
											break;
										case 2:
											%>Đang chạy<%
											break;
										case 3:
											%>Kết thúc<%
											break;
										case 4:
											%>Đã khóa<%
											break;
										case 5:
											%>Hoàn thành<%
											break;
										default:
											%>Chưa kích hoạt<%
											break;											
									}
									%>
	                            </td>
	                            <td class="center">
									<a title="Edit Link" href="/admin/deal/edit?id=<%=listMenuRight.get(i).getDealId()%>" class="edit" target="blank">
										Sửa
									</a>&nbsp;|							
									<a title="Remove Link" href="<%=listMenuRight.get(i).getDealId()%>" id="action_del" class="btnRemoveRight">
										Xóa khỏi menu
									</a>
								</td>
	                        </tr>
	                        <% } %>
	                        
	                    </tbody>
	                </table>
	               <%
                   	}
                       %>                
                </div><!--content-->
                
            </div><!--maincontentinner-->
            
            <jsp:include page="/admin/elements/copyright.jsp">
            	<jsp:param name="language" value="1" />
			</jsp:include>
            
        </div><!--maincontent-->
        
<jsp:include page="/admin/layouts/footer.jsp">
    <jsp:param name="language" value="1" />
</jsp:include>